<?php

// UBAH FORMAT d/m/Y ke Y-m-d
function ID_DateToMySQL($strDate)
{
	$strDateList = explode('/',$strDate);
	return $strDateList[2] . '-' . $strDateList[1] . '-' . $strDateList[0];
}

function month_range($d1,$d2,$sql=FALSE){
	$start = $month = strtotime($d1);
	$end = strtotime($d2);
	$months = array();
	while($month < $end)
	{
	     $months[] = $sql ? date('Y-m', $month) : date('M', $month);
	     $month = strtotime("+1 month", $month);
	}
	return $months;	  
}
function full_url($base64=false,$path=FALSE)
{
   $ci=& get_instance();
   if($path)
		$return = $ci->config->site_url();
	else
		$return = $ci->config->site_url().$ci->uri->uri_string();

   if(count($_GET) > 0)
   {
      $get =  array();
      foreach($_GET as $key => $val)
      {
         $get[] = $key.'='.$val;
      }
      $return .= '?'.implode('&',$get);
   }
   return $base64? base64_encode($return) : $return;
} 

function auto_include_view($set_category_grid=FALSE,$list=FALSE)
{
	$ci=& get_instance();

	if(isset($ci->override_view_path))
	{
		//die($ci->override_view_path);
		$ci->load->add_view_path($ci->override_view_path);
		//
		//$path = $ci->uri->segment(1) .'/'.$ci->uri->segment(2) . '/' . $ci->uri->segment(3); 
		$uri_arr = explode('/', $ci->uri->uri_string);
		$uri_add = '';

		if(in_array('edit', $uri_arr))
			$uri_add = 'edit';
		else if(in_array('add', $uri_arr))
			$uri_add = 'add';
		
		$path = $uri_add ;

		if(!$list){
			$auto_include_views = array(
				'form'	  => str_replace(array('add','edit'),'',$path).'form',
				'form_pre'	  => str_replace(array('add','edit'),'',$path).'form_pre',
				'context' => $path,
				'context_pre' => $path . '_pre',
			); 
		}else{
			$auto_include_views = array(
				'context' => $path . '_extra',
				'context_pre' => $path . '_pre',
				'override' =>  'list'
			); 
		}
	}
	else if($set_category_grid)
	{
		$uri_arr = explode('/', $ci->uri->uri_string);
		$uri_add = '';

		if(in_array('edit', $uri_arr))
			$uri_add = 'edit';
		else if(in_array('add', $uri_arr))
			$uri_add = 'add';
		
		$path = $ci->uri->segment(1) .'/'.$ci->uri->segment(2).'/'.$uri_add ;
	}
	else
	{
		$path = $ci->uri->segment(1) .'/'.$ci->uri->segment(2) . '/' . $ci->uri->segment(3); 
	}	
	if(!$list){
		$auto_include_views = array(
			'form'	  => str_replace(array('/add','/edit','/read'),'/',$path).'form',
			'form_pre'	  => str_replace(array('/add','/edit','/read'),'/',$path).'form_pre',
			'context' => $path,
			'context_pre' => $path . '_pre',
		); 
	}else{
		$auto_include_views = array(
			'context' => $path . '_extra',
			'context_pre' => $path . '_pre',
			'override' =>  $path . 'list'
		); 
	}
	// print_r($auto_include_views);
	// echo $ci->uri->uri_string;
	// die();
	return $auto_include_views; 
}


	

function ms_escape_string($data) {
        if ( !isset($data) or empty($data) ) return '';
        if ( is_numeric($data) ) return $data;

        $non_displayables = array(
            '/%0[0-8bcef]/',            // url encoded 00-08, 11, 12, 14, 15
            '/%1[0-9a-f]/',             // url encoded 16-31
            '/[\x00-\x08]/',            // 00-08
            '/\x0b/',                   // 11
            '/\x0c/',                   // 12
            '/[\x0e-\x1f]/'             // 14-31
        );
        foreach ( $non_displayables as $regex )
            $data = preg_replace( $regex, '', $data );
        $data = str_replace("'", "''", $data );
        return $data;
    }